package identify

import (
	"fmt"

	"yutooaisdk/languages"
)

// 识别模型自定义错误号
type Errno uint

const (
	ErrInitLib           Errno = iota + 1 // 初始化动态库失败
	ErrNotInitLib                         // 动态库未初始化
	ErrResNullPtr                         // 动态库方法返回空指针
	ErrResRecordIDEmpty                   // 识别模型返回的识别ID为空
	ErrResEmpty                           // 识别模型返回结果为空
	ErrCopyImageFail                      // 拷贝识别图片失败
	ErrRecog                              // 识别模型识别库出错
	ErrValidation                         // 识别模型加密库出错
	ErrAcceleration                       // 识别模型加速库出错
	ErrPreprocess                         // 识别模型预处理阶段出错
	ErrPostprocess                        // 识别模型后处理阶段出错
	ErrRecogPara                          // 识别模型传入参数错误
	ErrRecogMem                           // 识别模型内存错误
	ErrRecogData                          // 识别模型数据错误
	ErrRecogPipiline                      // 识别模型调用流程错误
	ErrRecogFile                          // 识别模型文件错误
	ErrKeyEmpty                           // 识别模型键不存在错误
	ErrJpegDataCorrupted                  // JPEG数据损坏
)

var errLangMap = map[Errno]map[languages.Code]string{
	ErrInitLib: {
		languages.EN_US: "Failed to initialize dynamic library",
		languages.ZH_CN: "初始化动态库失败",
	},
	ErrNotInitLib: {
		languages.EN_US: "Dynamic library not initialized",
		languages.ZH_CN: "动态库未初始化",
	},
	ErrResNullPtr: {
		languages.EN_US: "Dynamic library method returns null pointer",
		languages.ZH_CN: "动态库方法返回空指针",
	},
	ErrResRecordIDEmpty: {
		languages.EN_US: "The recognition ID returned by the recognition model is empty",
		languages.ZH_CN: "识别模型返回的识别ID为空",
	},
	ErrResEmpty: {
		languages.EN_US: "The recognition model returns an empty result",
		languages.ZH_CN: "识别模型返回结果为空",
	},
	ErrCopyImageFail: {
		languages.EN_US: "Copy recognition picture failed",
		languages.ZH_CN: "拷贝识别图片失败",
	},
	ErrRecog: {
		languages.EN_US: "Recognition Model Recognition Library Error",
		languages.ZH_CN: "识别模型识别库出错",
	},
	ErrValidation: {
		languages.EN_US: "Error in recognition model encryption library",
		languages.ZH_CN: "识别模型加密库出错",
	},
	ErrAcceleration: {
		languages.EN_US: "Recognition model acceleration library error",
		languages.ZH_CN: "识别模型加速库出错",
	},
	ErrPreprocess: {
		languages.EN_US: "An error occurred during the preprocessing phase of the recognition model",
		languages.ZH_CN: "识别模型预处理阶段出错",
	},
	ErrPostprocess: {
		languages.EN_US: "An error occurred during the post-processing phase of the recognition model",
		languages.ZH_CN: "识别模型后处理阶段出错",
	},
	ErrRecogPara: {
		languages.EN_US: "The input parameter of the recognition model is wrong",
		languages.ZH_CN: "识别模型传入参数错误",
	},
	ErrRecogMem: {
		languages.EN_US: "Identify model memory errors",
		languages.ZH_CN: "识别模型内存错误",
	},
	ErrRecogData: {
		languages.EN_US: "Identify model data errors",
		languages.ZH_CN: "识别模型数据错误",
	},
	ErrRecogPipiline: {
		languages.EN_US: "Identify model call process errors",
		languages.ZH_CN: "识别模型调用流程错误",
	},
	ErrRecogFile: {
		languages.EN_US: "Identify model file errors",
		languages.ZH_CN: "识别模型文件错误",
	},
	ErrKeyEmpty: {
		languages.EN_US: "Recognition model key does not exist error",
		languages.ZH_CN: "识别模型键不存在错误",
	},
	ErrJpegDataCorrupted: {
		languages.EN_US: "JPEG data corruption",
		languages.ZH_CN: "JPEG数据损坏",
	},
}

func (e Errno) Error() string {
	// 是否存在该错误
	errLang, ok := errLangMap[e]
	if !ok {
		return fmt.Sprintf("unknown identify model errno: %d", e)
	} else if errLang == nil {
		return fmt.Sprintf("raw identify model errno: %d", e)
	}
	// 是否存在对应语言
	errStr, ok := errLang[languages.GetEnv()]
	if ok {
		return errStr
	}
	// 优先使用英文
	errStr, ok = errLang[languages.EN_US]
	if ok {
		return errStr
	}
	// 次优先使用中文
	errStr, ok = errLang[languages.ZH_CN]
	if ok {
		return errStr
	}
	// 使用随机语言
	for _, v := range errLang {
		return v
	}
	// 不存在语言时
	return fmt.Sprintf("raw identify model errno: %d", e)
}
